var Page = function(){
    this.init();
};
Page.prototype = {
    params: {
        passport: '',
        userId:'',
        headImg:'',
        startTerm:'',
        endTerm:''
    },
    init: function(){
        this.initParams();
        this.initEvent();
    },
    initParams: function(){
        this.params.passport = Pub.getCookie('_PASSPORT');
        this.params.userId = Pub.getSession('userId');
    },
    getData: function(){
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport,
            userId:parseInt(_this.params.userId)
        };
        Api.userlistmodifydetails(paramsObj, function(data){
            console.log(data);
            _this.params.headImg=data.headPortrait;
            $('#mobile').val(data.mobile);
            $('#realName').val(data.realName);
            $('#idNumber').val(data.idNumber);
            $('#balance').val(data.balance);

            if (data.startTerm) {
                $('#time').val(data.startTerm+' ~ '+data.endTerm);
                _this.params.startTerm = data.startTerm+ ' 00:00:00';
                _this.params.endTerm = data.endTerm+ ' 00:00:00';
            };
            if (data.idcardFront) {
                $('#idcardFront').html('<img id="idcardFrontImg" class="mt-15" style="width:100px;height:100px;" src="'+data.idcardFront+'" >');
            };
            if (data.idcardBack) {
                $('#idcardBack').html('<img id="idcardBackImg" class="mt-15" style="width:100px;height:100px;" src="'+data.idcardBack+'" >');
            };

            if (data.isEnable==0) {
                honeySwitch.showOff("#isEnable");
            };
            if (data.isVip==0) {
                honeySwitch.showOff("#isVip");
            };
            if (data.isAnnualFee==0) {
                honeySwitch.showOff("#isAnnualFee");
            };
            if (data.isAuth==0) {
                honeySwitch.showOff("#isAuth");
            };


        });
    },
    uploadImg: function(){
        var _this = this;
        Api.upload('#up_form1','#file_1', 'idcardimgurl',_this.params.passport, function(response){
            console.log(response);
            if(response.code === 0){
                $('#idcardFront').html('<img id="idcardFrontImg" class="mt-15" style="width:100px;height:100px;" src="'+response.data.imgUrl+'" >');
            }
        });
        Api.upload('#up_form2','#file_2', 'idcardimgurl',_this.params.passport, function(response){
            console.log(response);
            if(response.code === 0){
                $('#idcardBack').html('<img id="idcardBackImg" class="mt-15" style="width:100px;height:100px;" src="'+response.data.imgUrl+'" alt="">');
            }
        });
        Api.upload('#up_form3','#file_3', 'headimgurl',_this.params.passport, function(response){
            console.log(response);
            if(response.code === 0){
                _this.params.headImg=response.data.imgUrl;
                layui.use('layer', function(){
                  layer.msg('上传成功');
                });
                console.log(_this.params.headImg);
            }
        });
    },
    submit:function(){
        var _this = this;
        $('#btn_submit').click(function(event) {
            var realName=$('#realName').val(),
                idNumber=$('#idNumber').val(),
                startTerm=_this.params.startTerm,
                endTerm=_this.params.endTerm,
                mobile=$('#mobile').val(),
                balance=$('#balance').val(),
                idcardFront=$('#idcardFrontImg').attr('src'),
                idcardBack=$('#idcardBackImg').attr('src'),
                headPortrait=_this.params.headImg;
                console.log(headPortrait);
            if ($('#isEnable').hasClass('switch-on')) {
                var isEnable=1;
            }else{
                var isEnable=0;
            };
            if ($('#isVip').hasClass('switch-on')) {
                var isVip=1;
            }else{
                var isVip=0;
            };
            if ($('#isAnnualFee').hasClass('switch-on')) {
                var isAnnualFee=1;
            }else{
                var isAnnualFee=0;
            };
            if ($('#isAuth').hasClass('switch-on')) {
                var isAuth=1;
            }else{
                var isAuth=0;
            };
            if (mobile=='') {
                layui.use('layer', function(){
                  layer.msg('请填写手机号',{icon: 5});
                });
            }else{
                var paramsObj = {
                    passport: _this.params.passport,
                    userId:_this.params.userId,
                    realName:realName,
                    idNumber:idNumber,
                    startTerm:startTerm,
                    endTerm:endTerm,
                    mobile:mobile,
                    idcardFront:idcardFront,
                    idcardBack:idcardBack,
                    headPortrait:headPortrait,
                    isVip:isVip,
                    isAnnualFee:isAnnualFee,
                    isAuth:isAuth,
                    balance:balance,
                    isEnable:isEnable
                };
                console.log(paramsObj);
                Api.userlistmodify(paramsObj, function(data){
                    console.log(data);
                    layui.use('layer', function(){
                      layer.msg('提交成功',{icon: 6});
                    });
                    setTimeout(function(){
                       window.location.href=Pub.getHtmlUrl('./user-list.html');
                    }, 1500)
                });
            };
        });
    },
    otherEvent:function(){
        var _this = this;
        $('.goods-checkbox input').iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue',
            increaseArea: '20%'
        });
        $('#btn_cancel').click(function(event) {
            window.location.href=Pub.getHtmlUrl('./user-list.html');
        });
        $('.deleteImg').click(function(event) {
            $("#"+$(this).attr('data')).html('<img class="mt-15" src="./images/imgno.png" style="width:100px;height:100px;" alt="">');
        });
        layui.use('laydate', function() {
            var laydate = layui.laydate;
            laydate.render({
                elem: '#time',
                range: '~',
                done: function(value, date, endDate) {
                    console.log(date);
                    if (date.year) {
                        if (date.month < 10) {
                            date.month = "0" + date.month;
                        }
                        if (date.date < 10) {
                            date.date = "0" + date.date;
                        }
                        if (endDate.month < 10) {
                            endDate.month = "0" + endDate.month;
                        }
                        if (endDate.date < 10) {
                            endDate.date = "0" + endDate.date;
                        }
                        var gmtStart = date.year + '-' + date.month + '-' + date.date + ' 00:00:00';
                        var gmtEnd = endDate.year + '-' + endDate.month + '-' + endDate.date + ' 00:00:00';
                        _this.params.startTerm = gmtStart;
                        _this.params.endTerm = gmtEnd;

                    } else {
                        _this.params.startTerm = '';
                        _this.params.endTerm = '';
                    };
                }
            });
        });
    },
    initEvent: function(){
        var _this = this;
         _this.getData();
        _this.otherEvent();
        _this.uploadImg();
        _this.submit();


    }
};
var page = new Page();